package com.admission.web.admin;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;
import org.xml.sax.SAXException;

import com.admission.model.RecruitmentDetail;
import com.admission.service.RecruitmentDetailManager;
import com.admission.util.DeleteFile;
import com.admission.util.FileFilter;
import com.admission.util.NewFileName;
import com.admission.util.PageUtils;
import com.admission.util.StaticRecruitment;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class RecruitmentDetailAdminAction extends
		CrudActionSupport<RecruitmentDetail> {

	private static final long serialVersionUID = 4723204571526589980L;
	private String id;
	private String ids;
	private RecruitmentDetail entity;
	private File[] recruitmentFile;
	private String[] recruitmentFileFileName;
	private RecruitmentDetailManager recruitmentDetailManager;
	private Page<RecruitmentDetail> page = new Page<RecruitmentDetail>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private String year;
	private String province;
	private String type;
	private StaticRecruitment staticRecruitment;

	private String uploadType;

	public RecruitmentDetail getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		recruitmentDetailManager.getTitleList(page);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		String uploadPath = ServletActionContext.getServletContext()
				.getRealPath("/upload"); // 上传文件保存的位置
		String[] subPath;
		try {
			if (recruitmentFile != null) {
				subPath = new String[recruitmentFile.length];
				for (int i = 0; i < recruitmentFile.length; i++) {
					subPath[i] = NewFileName.genNewFileName(
							recruitmentFileFileName[i], i);
					FileUtils.copyFile(recruitmentFile[i], new File(uploadPath,
							subPath[i]));
					recruitmentDetailManager.save(uploadPath + File.separator
							+ subPath[i], uploadType);
				}
			}
		} catch (Exception e) {
			if (uploadType.equals("1")) {
				this.addActionMessage("请按照艺术类录取统计模板表进行编辑，详细错误信息可在日志管理处查看，如果没有权限可联系超级管理员");
			} else {
				this.addActionMessage("请按照其他类型录取统计模板表进行编辑，详细错误信息可在日志管理处查看，如果没有权限可联系超级管理员");
			}
			return INPUT;
		}

		return list();
	}

	public String recovery() throws IOException, IllegalArgumentException, SAXException, IllegalAccessException {
		// 删除静态网页
		String regx = "recruitment-detail-.*";
		File f = new File(ServletActionContext.getServletContext().getRealPath(
				"/statichtml"));
		File[] s = f.listFiles(new FileFilter(regx));
		for (File file : s) {
			DeleteFile.deleteByFile(file);
		}
		staticRecruitment.createHtmlForRecruitmentDetail();
		Struts2Utils.renderText("刷新成功");
		return null;
	}

	@Override
	public String delete() throws Exception {
//		try {
			recruitmentDetailManager.delete(ServletActionContext
					.getServletContext().getRealPath("/statichtml"), year,
					province, type);
			// this.addActionMessage("操作成功");
//		} catch (Exception e) {
//			this.addActionMessage("操作失败，请重新操作");
//		}
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new RecruitmentDetail();
		} else {
			entity = recruitmentDetailManager.getById(id);
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public File[] getRecruitmentFile() {
		return recruitmentFile;
	}

	public void setRecruitmentFile(File[] recruitmentFile) {
		this.recruitmentFile = recruitmentFile;
	}

	public String[] getRecruitmentFileFileName() {
		return recruitmentFileFileName;
	}

	public void setRecruitmentFileFileName(String[] recruitmentFileFileName) {
		this.recruitmentFileFileName = recruitmentFileFileName;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Autowired
	public void setRecruitmentDetailManager(
			RecruitmentDetailManager recruitmentDetailManager) {
		this.recruitmentDetailManager = recruitmentDetailManager;
	}

	public RecruitmentDetail getEntity() {
		return entity;
	}

	public void setEntity(RecruitmentDetail entity) {
		this.entity = entity;
	}

	public Page<RecruitmentDetail> getPage() {
		return page;
	}

	public void setPage(Page<RecruitmentDetail> page) {
		this.page = page;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getYearOption() {
		List<String> l = recruitmentDetailManager.getYears();
		String r = "";
		for (String s : l) {
			r += "<option value='" + s + "' >" + s + "</option>";
		}
		return r;
	}

	public String getUploadType() {
		return uploadType;
	}

	public void setUploadType(String uploadType) {
		this.uploadType = uploadType;
	}

	@Autowired
	public void setStaticRecruitment(StaticRecruitment staticRecruitment) {
		this.staticRecruitment = staticRecruitment;
	}

}
